<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<link rel="stylesheet" type="text/css" href="../../../Themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="../../../Themes/icon.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/demo.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/common.css">
	</head>

	<body style="padding:0px;background-color: white">
		<div id="toolbar" class="receipts_toolbar_style">
		</div>
		<div>
			<div style="float: right;margin-top: 0px;max-width:60px;" id="notreview"></div>
			<form id="ff" action="#" method="post">
				<table cellpadding="5" class="displaytable" style="text-align: left;width: 80%;margin: 0 auto;">
					<tr>
						<td width="15%">
							<h id="star">* </h>编号</td>
						<td width="30%">
							<input class="easyui-textbox" style="width: 100%;" type="text" name="ORDER_NO" id="input_ORDER_NO" data-options="prompt:'由系统自动生成，不需填写',disabled:true"></input>
						</td>
						<td width="15%">
							<h id="star">* </h>检测日期</td>
						<td width="30%">
							<input class="easyui-datebox" style="width: 100%" type="text" id="input_CHECK_DATE" name="CHECK_DATE" />
						</td>

					</tr>
					<tr>
						<td>
							<h id="star">* </h>检测人</td>
						<td>
							<input class="easyui-textbox" style="width: 100%;" type="text" name="CHECK_MAN" id="input_CHECK_MAN"></input>
						</td>
					</tr>
					<tr class="hiddentr">
						<td >
							<h id="star">* </h>ID</td>
						<td >
							<input class="easyui-textbox" style="width: 100%;" type="text" name="ID" id="input_ID"></input>
						</td>
					</tr>
				</table>
				<table id="tt">

				</table>
				<div style="margin-left: 20%;" id="new_footer">

				</div>
			</form>
		</div>
		<div id="dlg" class="hiddenDialog">
			<iframe id="ifrmEdit" style="width: 100%;height: 100%;" frameborder="0" scrolling="no"></iframe>
		</div>
	</body>
	<script type="text/javascript" src="../../../JS/jquery.min.js"></script>
	<script type="text/javascript" src="../../../JS/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../../../JS/common.js"></script>
	<script type="text/javascript" src="../../../JS/finance.js"></script>
	<script type="text/javascript" src="../../../JS/jquery.edatagrid.js"></script>
	<script type="text/javascript" src="../../../JS/addRowShareFunction.js"></script>
	<script type="text/javascript">
		var currid = AddFlag;
		var id = getID(); //获取上个页面传回的ID
		var currPage = 0; //当前的页数
		var MaxPage = 0;
		var MinPage = 1;
		var _detailToolbarData;
		var updateData; //获取到的更新数据
		var _dataPage;
		var currRow;
		_detailToolbarData = AddDetailToolBar(7);
		$(document).ready(function(){
		if(id != AddFlag && id != undefined) {
			currid = id;
			loadRemote();
		}
		});
		///增加脚注
		getPageMaxAndMin();
		addFooter($("#new_footer"));

		var receipts_type = RECEIPT_FRONT_SYMBOL;
		receipts_type += RECEIPT_NEXT_SYMBOL;
		receipts_type += RECEIPT_ADD_SYMBOL;
		receipts_type += RECEIPT_SAVE_SYMBOL;
		receipts_type += RECEIPT_SEARCH_SYMBOL;
		receipts_type += RECEIPT_DELETE_SYMBOL;

		content = AddReceiptsToolBar(receipts_type, "");
		$("#toolbar").append(content);
		GetDetailTable();

		

		function GetDetailTable() {
			$('#tt').edatagrid({
				height: 310,
				toolbar: _detailToolbarData, //toolbar选择自定义的名为toolbar的工具栏
				collapsible: 'true',
				singleSelect: 'true',
				rownumbers: 'true',
				fitColumns: 'true',
				showFooter: 'true',
				autoSave: 'true',
				columns: [
					[{
							field: 'ID',
							title: 'ID',
							width: '0%',
							align: 'center'
						}, 
						{
							field: 'ORDER_ID',
							title: 'ORDER_ID',
							width: '0%',
							align: 'center'
						}, 
						{
							field: 'STOREHOUSE_NAME',
							title: '*库房名称',
							width: '50%',
							align: 'center',
							editor: {
								type: 'combobox',
								options: {
									valueField: 'NAME',
									textField: 'NAME',
									mode: 'remote',
									method: 'get',
									panelHeight: '100',
									url: RemoteLinkAddress + "/frozen/storehouse/listall.do?STATE=在用",
								}
							}
						}, {
							field: 'NOTES',
							title: '检测温度',
							width: '50%',
							align: 'center',
							editor: {
								type: 'textbox',
							}
						}, 
					]
				],
				onEndEdit: function(index, row, changes) { //在编辑完成或，执行该函数
					///当修改时触发此
					if(currid != AddFlag) {
						if(row.isNewRecord == false || row.isNewRecord == undefined) //即修改的情况
						{
							for(var i = 0; i < updateData.rows.length; i++) {
								if(updateData.rows[i].ID == row.ID) {
									var obj = updateData.rows[i];
									for(var s in obj) {
										if(obj[s] != row[s]) {
											obj[s] = row[s];
										}
									}
								}
							}
						} else {
							var len = updateData.rows.length;
							updateData.rows[len] = row;
							updateData.rows[len].ORDER_ID = currPage;
						}
					}
				},
				onDestroy: function(index, row) {
					if(currid != AddFlag) {
						for(var i = 0; i < updateData.rows.length; i++) {
							if(updateData.rows[i].ID == row.ID) {
								updateData.rows[i].ORDER_ID = ""; //直接替换
							}
						}
					}
				},
				onDblClickRow: function(index, row) {
					currRow = row;
				},
				onAdd: function(index, row) {
					currRow = row;
				}
			});
		}

		$('#tt').datagrid('hideColumn', 'ID');
		$('#tt').datagrid('hideColumn', 'ORDER_ID');

		function getPageMaxAndMin() { //获取最大页最小页
			var _url = RemoteLinkAddress + "/frozen/checkorder/getmaxminid.do";
			$.ajax({
				type: "get",
				url: _url,
				dataType: "json", //服务器返回的数据类型
				success: function(result) {
					if(result != null) {
						MaxPage = result.MAXID;
						MinPage = result.MINID;
						currPage = result.MINID + 1;
					}
				},
				error: function(xhr, status, error) {
					$.messager.alert("错误", "请求失败!");
				}
			});
		}
		function loadRemote() { //载入服务器上的数据，用于修改以及查看更多的时候调用，根据id是否等于-1来判断
			var _url = RemoteLinkAddress + '/frozen/checkorder/findbyid.do?ID=' + currid;
			getRemoteData(_url);
		}

		function getRemoteData(_url) //统一获取远程的数据
		{
			$.ajax({ //调用ajax方法，从服务器获取到该订单的所有数据
				type: "get",
				url: _url,
				dataType: "json",
				success: function(result) {
					if(result != null) {
						_dataPage = result;
						updateData = deepCopy(result);
						getFormData(result); //获取单据的头部和底部
						currPage = result.ID; //表示传回的ID
						currid = result.ID;
						var _data = result.rows; //将获取到的数据中字头为rows的数据赋值给_data
						$('#tt').edatagrid('loadData', _data); //调用easyui的loadData方法，将数据写入id为tt的datagrid中
						updateLog = "";

					}
				},
				error: function(xhr, status, error) {
					$.messager.alert("错误", "请求失败!");
				}
			});
		}
		//单据相关功能
		function frontpage() {
			if(currPage > MinPage) {
				var _url = RemoteLinkAddress + '/frozen/checkorder/pre.do?ID=' + currPage;
				getRemoteData(_url);
			} else {
				$.messager.alert("提示", "没有上张");
			}
		}

		function nextpage() {
			if(currPage < MaxPage) {
				var _url = RemoteLinkAddress + '/frozen/checkorder/next.do?ID=' + currPage;
				getRemoteData(_url);
			} else {
				$.messager.alert("提示", "没有下张");
			}
		}

		function newdata() { //新建
			currid = AddFlag;
			currPage = 0;
			_dataPage.ISVERIFY = 0;
			$("#input_STOREHOUSE_NAME").textbox("setValue", "");
			$("#input_STOREHOUSE_ID").textbox("setValue", "");
			$("#input_ORDER_NO").textbox("setValue", "");
			$("#input_ID").textbox("setValue", "");
			$('#tt').datagrid('loadData', {
				total: 0,
				rows: []
			});
			$('#notreview').empty();
		}

		function save() {
			if($("input[name='STOREHOUSE']").val() == "") {
				$.messager.alert('提示信息', '订单信息不能为空!');
			} else {
				if(currid != AddFlag) {
					getUpdateData();
				} else {
					getSaveData();
				}
			}
		}

		function submitData(_data) {
			var _url = "";
			if(currid == AddFlag) {
				_url = RemoteLinkAddress + "/frozen/checkorder/save.do";
			} else {
				_url = RemoteLinkAddress + "/frozen/checkorder/update.do";
			}
			var _senddata = JSON.stringify(_data);
			$.ajax({
				dataType: 'JSON',
				url: _url,
				data: 'a=' + _senddata,
				type: "POST",
				success: function(result) {
					if(currid == AddFlag) {
						var revData = result.msg.split(";");
						$("#input_ORDER_NO").textbox("setValue", revData[2]);
						$("#input_ID").textbox("setValue", revData[1]);
						$.messager.alert("提示", "新建保存成功");
						updateData = _data;
						currPage = revData[1];
						currid = revData[1];
						return;
					}
					$.messager.alert("提示", "修改保存成功");
				},
				error: function(result) {
					$.messager.alert("错误", "保存不成功");
				}
			});
		}

		function getSaveData() { //保存新增的数据
			var data = Object();
			data = submitFormData();
			data.rows = $("#tt").datagrid("getRows");
			submitData(data);
		}

		

		function getUpdateData() { //更新修改的数据
			updateData.CHECK_DATE= $("input[name='CHECK_DATE']").val();
			updateData.CHECK_MAN = $("input[name='CHECK_MAN']").val();
			updateData.ORDER_NO = $("input[name='ORDER_NO']").val();
			updateData.ID = $("input[name='ID']").val();
			updateData.LOG = updateLog;
			for(var i = 0; i < updateData.rows.length; i++) {
				var s = updateData.rows[i];
				if(s.UNIT_PRICE == "" && s.UNIT_PRICE == 0) {
					s.ORDER_ID = "";
				}
			}
			submitData(updateData);
		}

		function deletedata() { //删除
			if(_dataPage.ISVERIFY == "1") {
				$.messager.alert('警示', '已审核不可删除！');
				return;
			}
			if(currid == AddFlag) {
				$.messager.alert('警示', '该页面为新增页面不可删除！');
				return;
			}
			$.messager.confirm('Confirm', '是否删除', function(r) {
				if(r) {
					$.post(RemoteLinkAddress + "/frozen/checkorder/delete.do", {
							ID: currid,
						},
						function(result) {
							if(result.msg == "success") {
								nextpage();
								$.messager.alert({
									title: '成功',
									msg: '删除成功'
								});
							} else {
								$.messager.alert({
									title: 'Error',
									msg: result.msg
								});
							}
						},
						'json');
				}
			});
		}


		function searchmore() { //查询
			AddTabIn("气调库温度记录查询", parent.$('#tabs'), "UI/FrozenCave/WarehouseTemperature/WarehouseTemperatureList.html");
		}
	</script>